package org.example.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
public class RedissonConfig {

    @Value("${redisson.config.singleServerConfig.address}")
    private String address;

    @Value("${redisson.config.singleServerConfig.password}")
    private String password;

    @Value("${redisson.config.singleServerConfig.database}")
    private int database;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig
                .setAddress(address)
                .setDatabase(database);
        if (StringUtils.hasText(password)){
            singleServerConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

}
